package com.wh1200.mybatis.xmlless.model.component

import com.wh1200.mybatis.xmlless.model.MethodInfo
import com.wh1200.mybatis.xmlless.model.TableName


/**
 *
 * @author 吴昊
 * @date 2023/12/4 22:39
 * @since v0.0.0
 * @version 1.0
 */
class FromDeclaration(
    val tableName: TableName = TableName("", ""),
    val joins: List<JoinDeclaration> = listOf()
) : ISqlPart() {

  private var sql: String? = null

  companion object {
    private const val PREFIX = "FROM\n  "
  }

  override fun toSql(methodInfo: MethodInfo): String {
    if (sql != null) {
      return sql!!
    }
    if (joins.isEmpty()) {
      sql =  PREFIX + tableName.toSql()
    } else {
      sql = PREFIX + tableName.toSql() + "\n" + joins.joinToString("\n") { it.toSql(methodInfo) }
    }
    return sql!!
  }

  override fun toSql(): String {
    if (joins.isEmpty()) {
      return PREFIX + tableName.toSql()
    }
    return PREFIX + tableName.toSql() + "\n" + joins.joinToString("\n") { it.toSql() }
  }

}
